const http = require('http')
const fs = require('fs')
const path = require('path')

http.createServer((req, res)=>{
    console.log('request come', req.url)

    if (req.url === '/') {
        const html = fs.readFileSync(path.resolve(__dirname, 'test.html'),'utf8')
        res.writeHead(200,{
            "Content-Type": "text/html"
        })
    
        res.end(html)
    }

    if (req.url === '/script.js') {
        const etag = req.headers['if-none-match']
        if (etag === '777') {
            res.writeHeader(304, {
                'Content-Type': 'application/javacript',
                'Cache-Control': 'max-age=3,no-store',
                'Last-Modified': 123,
                'Etag': '777'
            })
            res.end('hhh')
        } else {
            res.writeHead(200,{
                'Content-Type': 'application/javacript',
                'Cache-Control': 'max-age=3',
                'Last-Modified': 123,
                'Etag': '777'
            })
            res.end('console.log("script loaded four")')
        }
    }
}).listen(8888)

console.log('server listening 8888')
